/**
 * <p>项目名：ViewPagerFragmentRadioGroup</p>
 * <p>包名：	com.my.viewpagerfragmentradiogroup</p>
 * <p>文件名：IndexFragment.java</p>
 * <p>版本信息： 2.1.0</p>
 * <p>日期： 2015/5/14/13:02.</p>
 * Copyright (c) 2015帮你公司-版权所有
 */
package com.tiantianyuegou.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.activity.BaseActivity;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.ShangJiaAdapter;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.ShangJiaModel;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.NetworkUtils;
import com.tiantianyuegou.view.refresh.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 商城
 */
public class IndexFragment extends Fragment implements View.OnClickListener{
// http://hjtech.wicp.net:8080/tiantianyuegou-api/business/list?cityId=1&countyId=1&scort=1

    TextView tv_default,tv_price,tv_salenum;
    View rootView;
    BaseActivity activity;
    private PullToRefreshListView refresh;
    private String TAG = "IndexFragment";
    private LoadingDialog loadingDialog;

    List<ShangJiaModel> data = new ArrayList<>();
    ShangJiaAdapter mAdapter;
    private ListView list_goods;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        activity = (BaseActivity) getActivity();
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_fenlei, container,
                    false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        findViewByIds();
        initdata();
        initListener();
        return rootView;
    }




    private void findViewByIds() {
        refresh = (PullToRefreshListView) rootView.findViewById(R.id.refresh);
        tv_default = (TextView) rootView.findViewById(R.id.tv_default);
        tv_price = (TextView) rootView.findViewById(R.id.tv_price);
        tv_salenum = (TextView) rootView.findViewById(R.id.tv_salenum);
        list_goods = (ListView) rootView.findViewById(R.id.list_goods);

    }
    private void initListener() {
        tv_default.setOnClickListener(this);
        tv_price.setOnClickListener(this);
        tv_salenum.setOnClickListener(this);
        tv_default.setTextColor(getResources().getColor(R.color.red));
        tv_price.setTextColor(getResources().getColor(R.color.black));
        tv_salenum.setTextColor(getResources().getColor(R.color.black));
        mAdapter = new ShangJiaAdapter(activity);
        list_goods.setAdapter(mAdapter);
        list_goods.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                当点击条目的时候将商家商品id进行传递，跳转到店铺详情界面
//                Intent intent = new Intent(activity, ShopDelitelActivity.class);
//                String tbId = data.get(position).getTbId();
//                intent.putExtra("tbId",tbId);
//                startActivity(intent);
            }
        });
    }
    private void initdata() {
        if (!NetworkUtils.isNetworkAvailable(activity)) {
            activity.showMsg("请检查网络");
        }
//        联网进行网络请求数据并展示数据
        loadingDialog = new LoadingDialog(activity);
        loadingDialog.showDialog();
        loadingDialog.setMsg("正在加载");
        RequestParams params = new RequestParams();
        params.addBodyParameter("cityId",1+"");
        activity.loadData(HttpRequest.HttpMethod.POST, ApiDefine.SHANGJIA_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                loadingDialog.dismiss();
                Log.e(TAG, "请求到的数据是" + responseInfo.result);
//                解析数据
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                   String code =  jsonObject.optString("code");
                    if (code.equalsIgnoreCase("100")){
                        JSONArray jsonArray = jsonObject.optJSONArray("list");
                        data.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject object = jsonArray.optJSONObject(i);
                            ShangJiaModel model = new ShangJiaModel();
//                            需要获取图片tb_logo，省市区，商品id,商家名称，商家主营tb_score
//                            http://hjtech.wicp.net:8080/tiantianyuegou-api/upload/business/20160918135331_555891.jpg
                            String tbLogo = object.optString("tb_logo");//商家logo
                            String tbPname = object.optString("pname");//省
                            String tbCname = object.optString("cname");//市
                            String tbOname = object.optString("oname");//区
                            String tbName = object.optString("tb_name");//商品名称
                            String tbScore = object.optString("tb_score");//主营
                            String tbId = object.optString("tb_id");//商品id
                            Log.e(TAG, "解析到的数据是" + tbLogo + tbPname + tbCname + tbOname + tbName + tbScore + tbId);
                            model.setTbName(tbName);
                            model.setTbPname(tbPname);
                            model.setTbId(tbId);
                            model.setTbLogo(tbLogo);
                            model.setTbCname(tbCname);
                            model.setTbOname(tbOname);
                            model.setTbScore(tbScore);
                            data.add(model);
                        }
                        mAdapter.setData(data);
                        mAdapter.notifyDataSetChanged();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                activity.showMsg("当前网络不可用，请检查你的网络设置");

            }
        });


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
//            默认
            case R.id.tv_default:
                tv_default.setTextColor(getResources().getColor(R.color.red));
                tv_price.setTextColor(getResources().getColor(R.color.black));
                tv_salenum.setTextColor(getResources().getColor(R.color.black));
                break;
//            区域
            case R.id.tv_price:
                tv_default.setTextColor(getResources().getColor(R.color.black));
                tv_price.setTextColor(getResources().getColor(R.color.red));
                tv_salenum.setTextColor(getResources().getColor(R.color.black));
                break;
//            时间
            case R.id.tv_salenum:
                tv_default.setTextColor(getResources().getColor(R.color.black));
                tv_price.setTextColor(getResources().getColor(R.color.black));
                tv_salenum.setTextColor(getResources().getColor(R.color.red));
                break;
        }
    }
}



